//
//  SlideView.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/1/8.
//  Copyright © 2016年 game. All rights reserved.
//

#import "SlideView.h"
#define HightOffatherOfbnt 40
@interface SlideView()<UIScrollViewDelegate>

@property (nonatomic,strong) UIView *slideOfBnt;

@property (weak, nonatomic) IBOutlet UIView *btnBac;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnBacheight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *LinHight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *certenLinWidth;

@property (weak, nonatomic) IBOutlet UIButton *slide1;
@property (weak, nonatomic) IBOutlet UIButton *slide2;
@end
@implementation SlideView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {

    }
    return self;
}
-(void)awakeFromNib{
    self.LinHight.constant = 0.5;
    self.certenLinWidth.constant = 0.5;
//    self.slide1.titleLabel.font = font(SCREEN_WIDTH*0.05);
//    self.slide2.titleLabel.font = font(SCREEN_WIDTH*0.05);

    
    _slideOfBnt = [[UIView alloc]initWithFrame:CGRectMake(0, HightOffatherOfbnt-1.2, SCREEN_WIDTH/2, 1.2)];
    _slideOfBnt.backgroundColor = mainColor;
    [self.btnBac addSubview:_slideOfBnt];

    self.scrollview.contentSize = CGSizeMake(SCREEN_WIDTH*2, 0);
    
    
    _scrollview.delegate = self;
    
//    _scrollview.scrollEnabled = NO;
  
}
- (IBAction)slideClick:(UIButton *)sender {
    
    
    for (int i=0; i<2; i++) {
        UIButton *bnt = [self.btnBac viewWithTag:i+10];
        [bnt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    [sender setTitleColor:mainColor forState:UIControlStateNormal];

    /*动画*/
    [UIView animateWithDuration:0.2 animations:^{
        self.slideOfBnt.x = SCREEN_WIDTH*(0.5*(sender.tag-10));
    }];
    
    [self.scrollview setContentOffset:CGPointMake(SCREEN_WIDTH*(sender.tag-10), 0) animated:YES];
    
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
   NSInteger  index= scrollView.contentOffset.x/SCREEN_WIDTH;
    
    for (int i=0; i<2; i++) {
        UIButton *bnt = [self.btnBac viewWithTag:i+10];
        [bnt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    UIButton *bbb = [self.btnBac viewWithTag:index+10];
    [bbb setTitleColor:mainColor forState:UIControlStateNormal];
    
    
    [UIView animateWithDuration:0.2 animations:^{
         self.slideOfBnt.x = SCREEN_WIDTH*(0.5*index);
    }];


}

-(void)setTitleAry:(NSArray *)titleAry{
    
    _titleAry = [titleAry copy];
    
    for (int i=0; i<2; i++) {
        UIButton *bnt = [self.btnBac viewWithTag:i+10];
        [bnt setTitle:_titleAry[i] forState:UIControlStateNormal];
    }
}

@end
